﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

using DataTransferObject;
using DataAccessLayer;
using BusinessLogicLayer;

namespace PresentationLayer
{
    public partial class WizardMarketingPaymentNew : Form
    {
        private MarketingPaymentMaster marketingPaymentMaster;
        private int marketingProgramID;
        private int marketingIncentiveID;
        private int marketingPaymentTermID;
        private int customerID;

        private Binding marketingProgramIDBinding;
        private Binding marketingIncentiveIDBinding;
        private Binding marketingPaymentTermIDBinding;
        private Binding customerIDBinding;

        public int MarketingProgramID
        {
            get { return this.marketingProgramID; }
            set
            {
                if (this.marketingProgramID != value)
                {
                    this.marketingProgramID = value;
                    this.marketingIncentiveID = -1;

                    this.comboBoxMarketingIncentiveID.DataSource = this.TableMarketingIncentiveID();
                    this.comboBoxMarketingIncentiveID.DisplayMember = "PaymentPeriod";
                    this.comboBoxMarketingIncentiveID.ValueMember = "MarketingIncentiveID";
                }
            }
        }

        public int MarketingIncentiveID
        {
            get { return this.marketingIncentiveID; }
            set
            {
                if (this.marketingIncentiveID != value)
                {
                    this.marketingIncentiveID = value;
                    this.marketingPaymentTermID = -1; //Reset

                    this.comboBoxMarketingPaymentTermID.DataSource = this.TableMarketingPaymentTermID();
                    this.comboBoxMarketingPaymentTermID.DisplayMember = "Description";
                    this.comboBoxMarketingPaymentTermID.ValueMember = "MarketingPaymentTermID";

                }
            }
        }

        public int MarketingPaymentTermID
        {
            get { return this.marketingPaymentTermID; }
            set
            {
                if (this.marketingPaymentTermID != value)
                {
                    this.marketingPaymentTermID = value;

                    this.comboBoxCustomerID.DataSource = this.TableCustomerID();
                    this.comboBoxCustomerID.DisplayMember = "Description";
                    this.comboBoxCustomerID.ValueMember = "CustomerID";

                }
            }
        }

        public int CustomerID
        {
            get { return this.customerID; }
            set { this.customerID = value; }
        }


        public WizardMarketingPaymentNew(MarketingPaymentMaster marketingPaymentMaster)
        {
            InitializeComponent();

            try
            {
                this.marketingPaymentMaster = marketingPaymentMaster;

                this.MarketingProgramID = this.marketingPaymentMaster.MarketingProgramID;
                this.MarketingIncentiveID = this.marketingPaymentMaster.MarketingIncentiveID;
                this.MarketingPaymentTermID = this.marketingPaymentMaster.MarketingPaymentTermID;
                this.CustomerID = this.marketingPaymentMaster.CustomerID;

                this.comboBoxMarketingProgramID.DataSource = this.TableMarketingProgramID();
                this.comboBoxMarketingProgramID.DisplayMember = "Reference";
                this.comboBoxMarketingProgramID.ValueMember = "MarketingProgramID";


                this.marketingProgramIDBinding = this.comboBoxMarketingProgramID.DataBindings.Add("SelectedValue", this, "MarketingProgramID", true, DataSourceUpdateMode.OnPropertyChanged);
                this.marketingIncentiveIDBinding = this.comboBoxMarketingIncentiveID.DataBindings.Add("SelectedValue", this, "MarketingIncentiveID", true, DataSourceUpdateMode.OnPropertyChanged);
                this.marketingPaymentTermIDBinding = this.comboBoxMarketingPaymentTermID.DataBindings.Add("SelectedValue", this, "MarketingPaymentTermID", true, DataSourceUpdateMode.OnPropertyChanged);
                this.customerIDBinding = this.comboBoxCustomerID.DataBindings.Add("SelectedValue", this, "CustomerID", true, DataSourceUpdateMode.OnPropertyChanged);

                this.marketingProgramIDBinding.BindingComplete += new BindingCompleteEventHandler(marketingProgramIDBinding_BindingComplete);
                this.marketingIncentiveIDBinding.BindingComplete += new BindingCompleteEventHandler(marketingIncentiveIDBinding_BindingComplete);
                this.marketingPaymentTermIDBinding.BindingComplete += new BindingCompleteEventHandler(marketingPaymentTermIDBinding_BindingComplete);
                this.customerIDBinding.BindingComplete += new BindingCompleteEventHandler(customerIDBinding_BindingComplete);

            }
            catch (Exception exception)
            {
                GlobalExceptionHandler.ShowExceptionMessageBox(this, exception);
            }
        }



        private void marketingProgramIDBinding_BindingComplete(object sender, BindingCompleteEventArgs e)
        {
            if (e.BindingCompleteState != BindingCompleteState.Success) { this.MarketingProgramID = 0; }
        }

        void marketingIncentiveIDBinding_BindingComplete(object sender, BindingCompleteEventArgs e)
        {
            if (e.BindingCompleteState != BindingCompleteState.Success) { this.MarketingIncentiveID = 0; }
        }

        void marketingPaymentTermIDBinding_BindingComplete(object sender, BindingCompleteEventArgs e)
        {
            if (e.BindingCompleteState != BindingCompleteState.Success) { this.MarketingPaymentTermID = 0; }
        }

        void customerIDBinding_BindingComplete(object sender, BindingCompleteEventArgs e)
        {
            if (e.BindingCompleteState != BindingCompleteState.Success) { this.CustomerID = 0; }
        }






        private DataTable TableMarketingProgramID()
        {
            DataTable tableMarketingProgramID = SQLDatabase.GetDataTable("SELECT DISTINCT MarketingProgramMaster.MarketingProgramID, MarketingProgramMaster.Reference FROM MarketingIncentiveDetail INNER JOIN MarketingProgramMaster ON MarketingIncentiveDetail.MarketingProgramID = MarketingProgramMaster.MarketingProgramID WHERE MarketingProgramMaster.MarketingProgramID = " + this.MarketingProgramID + " OR ROUND(MarketingIncentiveDetail.IncentiveAmount - MarketingIncentiveDetail.PaidAmount, 0) > 0 ");

            return tableMarketingProgramID;
        }

        private DataTable TableMarketingIncentiveID()
        {
            DataTable tableMarketingIncentiveID = SQLDatabase.GetDataTable("SELECT DISTINCT MarketingIncentiveMaster.MarketingIncentiveID, MarketingIncentiveMaster.PaymentPeriod FROM MarketingIncentiveMaster INNER JOIN MarketingIncentiveDetail ON MarketingIncentiveMaster.MarketingIncentiveID = MarketingIncentiveDetail.MarketingIncentiveID WHERE MarketingIncentiveMaster.MarketingProgramID = " + this.MarketingProgramID + " AND (MarketingIncentiveMaster.MarketingIncentiveID = " + this.MarketingIncentiveID + " OR ROUND(MarketingIncentiveDetail.IncentiveAmount - MarketingIncentiveDetail.PaidAmount, 0) > 0)");
            DataRow rowMarketingIncentiveID = tableMarketingIncentiveID.NewRow(); rowMarketingIncentiveID["MarketingIncentiveID"] = 0; rowMarketingIncentiveID["PaymentPeriod"] = "[All Periods]"; tableMarketingIncentiveID.Rows.Add(rowMarketingIncentiveID);

            return tableMarketingIncentiveID;
        }

        private DataTable TableMarketingPaymentTermID()
        {
            DataTable tableMarketingPaymentTermID = SQLDatabase.GetDataTable("SELECT DISTINCT ListMarketingPaymentTerm.MarketingPaymentTermID, ListMarketingPaymentTerm.Description FROM MarketingIncentiveDetail INNER JOIN ListMarketingPaymentTerm ON MarketingIncentiveDetail.MarketingPaymentTermID = ListMarketingPaymentTerm.MarketingPaymentTermID WHERE MarketingIncentiveDetail.MarketingProgramID = " + this.MarketingProgramID + (this.MarketingIncentiveID != 0 ? " AND MarketingIncentiveDetail.MarketingIncentiveID = " + this.MarketingIncentiveID : "") + " AND (MarketingIncentiveDetail.MarketingPaymentTermID = " + this.MarketingPaymentTermID + " OR ROUND(MarketingIncentiveDetail.IncentiveAmount - MarketingIncentiveDetail.PaidAmount, 0) > 0)");
            DataRow rowMarketingPaymentTermID = tableMarketingPaymentTermID.NewRow(); rowMarketingPaymentTermID["MarketingPaymentTermID"] = 0; rowMarketingPaymentTermID["Description"] = "[All Payment Terms]"; tableMarketingPaymentTermID.Rows.Add(rowMarketingPaymentTermID);

            return tableMarketingPaymentTermID;
        }

        private DataTable TableCustomerID()
        {
            DataTable tableCustomerID = SQLDatabase.GetDataTable("SELECT DISTINCT ListCustomerName.CustomerID, ListCustomerName.Description FROM MarketingIncentiveDetail INNER JOIN ListCustomerName ON MarketingIncentiveDetail.CustomerID = ListCustomerName.CustomerID WHERE MarketingIncentiveDetail.MarketingProgramID = " + this.MarketingProgramID + (this.MarketingIncentiveID != 0 ? " AND MarketingIncentiveDetail.MarketingIncentiveID = " + this.MarketingIncentiveID : "") + (this.MarketingPaymentTermID != 0 ? " AND MarketingIncentiveDetail.MarketingPaymentTermID = " + this.MarketingPaymentTermID : "") + " AND (MarketingIncentiveDetail.CustomerID = " + this.CustomerID + " OR ROUND(MarketingIncentiveDetail.IncentiveAmount - MarketingIncentiveDetail.PaidAmount, 0) > 0)");
            DataRow rowCustomerID = tableCustomerID.NewRow(); rowCustomerID["CustomerID"] = 0; rowCustomerID["Description"] = "[All Customers]"; tableCustomerID.Rows.InsertAt(rowCustomerID, 0);

            return tableCustomerID;
        }


        private void toolStripButtonEscapeAndOK_Click(object sender, EventArgs e)
        {
            try
            {
                if (sender.Equals(this.toolStripButtonOK))
                {
                    this.marketingPaymentMaster.MarketingIncentiveID = this.MarketingIncentiveID;
                    this.marketingPaymentMaster.MarketingIncentivePeriod = this.MarketingIncentiveID != 0 ? this.comboBoxMarketingIncentiveID.Text.Replace("[", "").Replace("]", "") : "";

                    this.marketingPaymentMaster.MarketingPaymentTermID = this.MarketingPaymentTermID;
                    this.marketingPaymentMaster.MarketingPaymentTermDescription = this.MarketingPaymentTermID != 0 ? this.comboBoxMarketingPaymentTermID.Text.Replace("[", "").Replace("]", "") : "";

                    this.marketingPaymentMaster.CustomerID = this.CustomerID;
                    this.marketingPaymentMaster.CustomerDescription = this.CustomerID != 0 ? this.comboBoxCustomerID.Text.Replace("[", "").Replace("]", "") : "";


                    this.marketingPaymentMaster.MarketingProgramID = this.MarketingProgramID;
                    this.marketingPaymentMaster.MarketingProgramReference = ""; this.marketingPaymentMaster.MarketingProgramReference = this.comboBoxMarketingProgramID.Text;
                }

                this.DialogResult = sender.Equals(this.toolStripButtonOK) ? DialogResult.OK : DialogResult.Cancel;
            }
            catch (Exception exception)
            {
                GlobalExceptionHandler.ShowExceptionMessageBox(this, exception);
            }
        }




    }
}
